package com.cn.appcore.http;


import android.util.Log;

import androidx.annotation.NonNull;


import com.cn.appcore.utils.NLog;

import org.json.JSONException;
import org.json.JSONObject;

import okhttp3.Response;
import okhttp3.ResponseBody;

public class HandleErrorInterceptor extends ResponseBodyInterceptor {

    @Override
    Response intercept(@NonNull Response response, String url, String body) {
        try {
            String json = body;
            NLog.INSTANCE.d("接口返回", "║content 返回: " + json);
            JSONObject jsonObject = new JSONObject(json);
            if ("0".equals(jsonObject.optString("errcode"))) {
                if (response.body() != null) {
                    JSONObject obj = new JSONObject(json);
                    if(!obj.has("data")){
                        obj.put("data","{}");
                    }
                    ResponseBody boy = ResponseBody.create(response.body().contentType(), obj.toString());
                    return response.newBuilder().body(boy).build();
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return response;
    }
}